package org.csu.petstore_manage.service;

import org.csu.petstore_manage.domain.Category;
import org.csu.petstore_manage.domain.Item;
import org.csu.petstore_manage.domain.Product;
import org.csu.petstore_manage.persistence.CategoryMapper;
import org.csu.petstore_manage.persistence.ItemMapper;
import org.csu.petstore_manage.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public void addCategory(String categoryId,String categoryName,String categoryDescn){
        categoryMapper.addCategory(categoryId,categoryName,categoryDescn);
    }
    public void updateProduct(Product newProduct){

        productMapper.updateProduct(newProduct);
        System.out.println("updateproduct");
    }

    public void updateItem(Item newItem){
        productMapper.updateItemquantity(newItem);
        productMapper.updateItem(newItem);
    }

    public void addItem(Item newItem){
        productMapper.addItem(newItem);
        productMapper.addItemQuantity(newItem);
    }

    public void addProduct(Product newProduct){
        productMapper.addProduct(newProduct);
    }

    public void deleteProduct(String productId){
        productMapper.deleteProduct(productId);
    }

    public void deleteItem(String itemId){
        productMapper.deleteItem(itemId);
    }

    public List<Product> getProducts() {
        return productMapper.getProducts();
    }

    public List<Product> getProductByCategoryId(String categoryId) {
        return productMapper.getProductByCategory(categoryId);
    }

    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> getItemListByProduct(String productId){
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId){
        return itemMapper.getItem(itemId);
    }

    public boolean isItemInStock(String itemId,int num){
        return itemMapper.getInventoryQuantity(itemId) >= num;
    }

}
